// pages/my/index.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "个人中心", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: false, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,

    authData: {
      authType: '0', //0表示使用登录弹窗  1表示使用完善昵称头像弹窗 2表示直接弹出头像昵称选择框
      authUsername: "false", //是否已经授权头像昵称
    },
    showLogin: false, //是否显示授权弹窗
    showWsInfoDom: false, //直接显示头像昵称弹窗
    showPrivacyFlag: app.globalData.showPrivacyFlag,

    openid: '',
    nickname: '',
    head: '',
    shareTitle: '',
    shareImg: '',
    mySf: '',
    is_register: 0,
    tipsContent: '',
    is_nickAndHead: 0,
    isOneShow: 1,
    orderTotal: 0,
    welfareNum: 0,
    familyNum: 0,
    isStatus: 0,
    showBirth: false,
    headerType: 0,
    isGetZzBadge: 0, //是否获取了长者徽章
    isGetRzBadge: 0, //是否获取了入住徽章
  },

  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //获取缓存中的openid'oZYOb4ktAvmrb5TsIPBHlrGEKcZg'
    var openid = wx.getStorageSync('ly24_openid');
    // var openid = 'oZYOb4jQ1cdq3ZdTqRcQX4r_BI5Y';
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getMyCenterPageInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getMyCenterPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取登录code
  getMyCenterPageInfo(openid) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          that.getMyCenterPageData(openid, res.code);
        } else {
          that.getMyCenterPageData(openid, '');
        }
      },
      fail: function () {
        that.getMyCenterPageData(openid, '');
      }
    });
  },

  //获取本页数据
  getMyCenterPageData(openid, code) {
    var that = this;
    //获取发放权益的缓存
    var is_send_welfare = 0;
    var sendTimestamp = wx.getStorageSync('sendTimestamp');
    if (sendTimestamp == '' || sendTimestamp == null || sendTimestamp == undefined) {
      var timestamp = new Date().getTime();
      //缓存权益发放标识
      wx.setStorageSync('sendTimestamp', timestamp);
      is_send_welfare = 1;
    } else {
      //存在发放标识
      var timestamp = new Date().getTime();
      if (timestamp - sendTimestamp > 10000) {
        //缓存已超过10s
        is_send_welfare = 1;
         //重置发放时间戳
        wx.setStorageSync('sendTimestamp', timestamp);
      }
    }
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getMyCenterPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'code': code,
        'is_send_welfare': is_send_welfare
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          //判断是否登录注册
          if (res.data.is_register == '0') {
            wx.showModal({
              title: '温馨提示',
              content: res.data.tipsContent,
              complete: (res) => {
                if (res.confirm) {
                  //跳转注册界面
                  wx.setStorageSync('loginHeaderUrl', '/pages/my/index');
                  wx.setStorageSync('loginHeaderType', 'switchTab');
                  if (res.confirm) {
                    //跳转注册界面
                    wx.redirectTo({
                      url: '/pages/login/index'
                    });
                  }
                }
              }
            })
          }

          //$isStatus=0 不显示
          //$isStatus=1 显示 但未抽奖
          //$isStatus=2 显示 已抽奖
          //$isStatus=3 不显示 过期
          //$isStatus = 4 已兑换 已抽奖
          that.setData({
            is_register: res.data.is_register,
            tipsContent: res.data.tipsContent,
            nickname: res.data.nickname,
            head: res.data.head,
            mySf: res.data.mySf,
            is_nickAndHead: res.data.is_nickAndHead,
            orderTotal: res.data.orderTotal,
            welfareNum: res.data.welfareNum,
            familyNum: res.data.familyNum,
            isStatus: res.data.isStatus,
            headerType: res.data.headerType,
            isGetZzBadge: res.data.isGetZzBadge,
            isGetRzBadge: res.data.isGetRzBadge
          });

          //判断是否显示生日弹窗
          if (res.data.isStatus == 2 || res.data.isStatus == 1) {
            that.setData({
              showBirth: true
            });
          }
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  //处理完善信息--包含隐私协议
  addWxInfo() {
    var that = this;
    //判断是否登录注册
    if (that.data.is_register == '0') {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.setStorageSync('loginHeaderUrl', '/pages/my/index');
            wx.setStorageSync('loginHeaderType', 'switchTab');
            if (res.confirm) {
              //跳转注册界面
              wx.redirectTo({
                url: '/pages/login/index'
              });
            }
          }
        }
      })
      return;
    }
    this.setData({
      ['authData.authType']: 1,
      showLogin: true
    });
  },

  //处理完善信息--不包含隐私协议
  addWxInfo2() {
    var that = this;
    //判断是否登录注册
    if (that.data.is_register == '0') {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.setStorageSync('loginHeaderUrl', '/pages/my/index');
            wx.setStorageSync('loginHeaderType', 'switchTab');
            if (res.confirm) {
              //跳转注册界面
              wx.redirectTo({
                url: '/pages/login/index'
              });
            }
          }
        }
      })
      return;
    }
    this.setData({
      showWsInfoDom: true
    });
  },

  //接收子组件电话授权及头像昵称授权后后返回的值
  returnVal(e) {
    var that = this;
    if (that.data.nickname != '') {
      app.showTip('个人信息修改成功');
    } else {
      app.showTip('个人信息已完善');
    }

    //app.showTip(e.detail.head);
    //授权完头像昵称
    this.setData({
      is_nickAndHead: e.detail.is_nickAndHead,
      head: that.data.baseUrl + e.detail.head,
      nickname: e.detail.nickname
    });


  },

  //跳转至各个栏目
  goDetail(e) {
    var that = this;
    //判断是否进行过登录认证
    //判断是否登录注册
    if (that.data.is_register == '0') {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.setStorageSync('loginHeaderUrl', '/pages/my/index');
            wx.setStorageSync('loginHeaderType', 'switchTab');
            if (res.confirm) {
              //跳转注册界面
              wx.redirectTo({
                url: '/pages/login/index'
              });
            }
          }
        }
      })
      return;
    }
    var flag = e.currentTarget.dataset.flag;
    if (flag == 'wy') {
      //跳转物业
      wx.navigateTo({
        url: '/pages/lifeService/managementFee/menu'
      });
    } else if (flag == 'myHouse') {
      //跳转我的房产
      wx.navigateTo({
        url: '/pages/my/myHouse'
      });
    } else if (flag == 'order') {
      //跳转订单详情
      wx.navigateTo({
        url: '/pages/my/orderList'
      });
    } else if (flag == 'welfare') {
      //跳转我的权益
      wx.navigateTo({
        url: '/pages/equity/index'
      });
    } else if (flag == 'square') {
      //跳转我的广场
      wx.navigateTo({
        url: '/pages/square/my'
      });
    } else if (flag == 'family' && that.data.mySf != '游客') {
      //跳转我的家人
      wx.navigateTo({
        url: '/pages/my/familyManage'
      });
    } else if (flag == 'yzrz') {
      //跳转业主认证
      wx.navigateTo({
        url: '/pages/my/ownerValidate'
      });
    } else if (flag == 'contact') {
      //跳转联系我们
      wx.navigateTo({
        url: '/pages/my/contact'
      });
    } else if (flag == 'ts') {
      //跳转投诉建议
      wx.navigateTo({
        url: '/pages/my/leaveMsg'
      });
    } else if (flag === 'change') {
      // 变更手机号
      wx.navigateTo({
        url: '/pages/phone-change/current-phone/index'
      });
      /*wx.navigateTo({
        url: '/pages/phone-change/newPhoneBind/index?isOldTelCheck=1'
      });*/
    }
  },

  //退出登录
  logOut() {
    var that = this;

    //执行退出登录
    wx.showModal({
      title: '温馨提示',
      content: '您确认退出本次登录?',
      complete: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '退出登录中...'
          });
          //后台请求时段是否满了
          wx.request({
            url: that.data.baseUrl + 'logout.php',
            method: 'POST',
            data: {
              openid: that.data.openid
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              if (res.data.status == 1) {
                //重新获取页面数据
                that.getMyCenterPageInfo(that.data.openid);
              } else {
                app.showTip(res.data.msg);
              }
            },
            fail: function () {
              wx.hideLoading();
              app.showTip(服务器繁忙, 请稍候重试);
            }
          });
        }
      }
    })

  },

  //跳转生日预约
  goBirthDay() {
    var that = this;
    if (that.data.isStatus == 1) {
      //跳转抽奖页面
      wx.navigateTo({
        url: '/pages/my/birthday'
      });
      return;
    }
    if (that.data.headerType == 0) {
      //跳转预约页面
      wx.navigateTo({
        url: '/pages/my/birthdayOrder'
      });
    } else {
      //跳转预约成功页面
      wx.navigateTo({
        url: '/pages/my/birthdayOrderResult'
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 4,
      })
    }

    if (that.data.isOneShow == 0) {
      //不是第一次进来，重新刷新页面数据
      that.getMyCenterPageData(that.data.openid, '');
    }

    //处理是不是第一次进小程序
    if (that.data.isOneShow == 1) {
      that.setData({
        isOneShow: 0
      });
    }

    //更新showPrivacyFlag
    this.setData({
      showPrivacyFlag: app.globalData.showPrivacyFlag
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})